﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using libUsbContorl;
using System.Configuration;


namespace AllInOne.Utils
{
    public class UsbPosPrintApi
    {
        public static bool initFlag = false;
        private static UsbOperation printer = new UsbOperation();

        public static void InitProperties()
        {
            AppSettingsReader reader = new AppSettingsReader();
            POS_INDEX = (int)reader.GetValue("POS_INDEX", typeof(int));
        }

        private static int POS_INDEX = -1;
        public static bool openPrinter()
        {
            printer.FindUSBPrinter();
            
            if (printer.USBPortCount > 0)
            {

                    initFlag = printer.LinkUSB(POS_INDEX);
                    return initFlag;
                
            }
            return false;
        }


        public static void nextLine(int line)
        {
            for (int i = 0; i < line; i++)
            {
                nextLine();
            }
        }
        public static void nextLine()
        {
            sendData2USB(new byte[] { 0x0a });
        }
        public static void cutPaper()
        {
            sendData2USB(new byte[] { 0x1d, 0x56, 0x00 });
        }

        public static void setFontSize(int size)
        {
            byte bSize = (byte)(size * 17);
            sendData2USB(new byte[] { 0x1d, 0x21, bSize });
        }


       

        public static void reset() {
            sendData2USB(new byte[] { 0x1b, 0x40 });
        }

        public static void setAlign(Alignment alignment)
        {
            sendData2USB(new byte[] { 0x1b, 0x61, (byte)alignment });
        }




        public static void printBarcode(string barcode)
        {
            sendData2USB(new byte[] { 0x1b, 0x40 });

            //设置条码居中
            sendData2USB(new byte[] { 0x1b, 0x61, 0x01 });
            //设置数字在条码下方
            sendData2USB(new byte[] { 0x1d, 0x48, 0x02 });

            //设置条码高度
            sendData2USB(new byte[] { 0x1d, 0x68, 0x50, 0x00 });

            //设置条码宽度
            sendData2USB(new byte[] { 0x1d, 0x77, 0x02,0x00 });

            //打印条码



            var data = new byte[barcode.Length+4];
            //设置条码类型为Code128
            new byte[] { 0x1d, 0x6b, 0x49, 0x0d }.CopyTo(data,0);

            //条码内容
            byte[] byteArray = Encoding.UTF8.GetBytes(barcode);
            byteArray.CopyTo(data, 4);
            
            //打印条码
            sendData2USB(data);
        }

        public static void sendData2USB(byte[] str)
        {
            printer.SendData2USB(str, str.Length);
        }
        public static void sendData2USB(string str)
        {
            byte[] by_SendData = System.Text.Encoding.Default.GetBytes(str);
            sendData2USB(by_SendData);
        }


        public static void printTicket(string lsh)
        {
            if (openPrinter())
            {
                reset();
                setAlign(Alignment.CENTER);
                nextLine();
                setFontSize(1);
                sendData2USB("缴费凭证");
                nextLine();
                setFontSize(0);
                sendData2USB("--------------------------------");
                nextLine();
                sendData2USB("流 水 号:");
                nextLine();
                printBarcode(lsh);
                nextLine();
                sendData2USB("--------------------------------");
                nextLine(5);
                cutPaper();
                System.Threading.Thread.Sleep(100);
                nextLine(2);

            }
        }
    }

    public enum Alignment {
        LEFT = 0,
        CENTER = 1,
        RIGHT = 2
    }
}
